package com.gin.no_gay_alliance.fragment.main;

import android.annotation.SuppressLint;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.gin.nga.response.forum.ForumGroup;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.databinding.FragmentMainHomeBinding;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.fragment.forum.ForumFavorFragment;
import com.gin.no_gay_alliance.fragment.forum.ForumIndexFragment;
import com.gin.no_gay_alliance.fragment.forum.ForumSearchFragment;
import com.gin.no_gay_alliance.manager.provider.ResourceManager;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页，版面搜索，收藏版面，首页版面
 */
public class MainHomeFragment extends BaseBindingFragment<FragmentMainHomeBinding> {
    FragmentStateAdapter fragmentStateAdapter;
    // TODO: 2023/9/20 替换为model
    /**
     * 版面分组数据
     */
    List<ForumGroup> forumGroups = new ArrayList<>();
    ForumFavorFragment forumFavorFragment;

    /**
     * 固定fragment的数量
     */
    int staticFragmentCount = 2;

    boolean paused = false;
    private TabLayoutMediator tabLayoutMediator;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        // 适配器
        fragmentStateAdapter = new FragmentStateAdapter(this) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                switch (position) {
                    case 0:
                        return new ForumSearchFragment();
                    case 1:
                        forumFavorFragment = new ForumFavorFragment();
                        return forumFavorFragment;
                    default:
                        ForumIndexFragment fragment = new ForumIndexFragment();
                        fragment.setForumGroup(forumGroups.get(position - staticFragmentCount));
                        return fragment;
                }
            }

            @Override
            public int getItemCount() {
                return forumGroups.size() + staticFragmentCount;
            }
        };
        b.vp2.setAdapter(fragmentStateAdapter);
        // 预加载页面数量
        b.vp2.setOffscreenPageLimit(1);
        b.vp2.setCurrentItem(1, false);
        // 绑定tabLayout
        tabLayoutMediator = attachTabLayout(tabLayoutMediator, b.tabLayout, b.vp2);
    }

    /**
     * 配置 TabLayout
     *
     * @param tab      标签
     * @param position 位置
     */
    @Override
    public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
        // 配置每个标签
        switch (position) {
            case 0:
                tab.setText("搜索版面");
                break;
            case 1:
                tab.setText("收藏版面");
                break;
            default:
                tab.setText(forumGroups.get(position - staticFragmentCount).getName());
        }
    }

    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        inflater.inflate(R.menu.base_option_menu, menu);
        menu.findItem(R.id.menu_item_noti).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onResume() {
        super.onResume();

        // 请求版面信息
        ResourceManager.bbsIndexProvider().async(false, groups -> {
            forumGroups.clear();
            forumGroups.addAll(groups);
            ui(() -> fragmentStateAdapter.notifyDataSetChanged());
        });

        // 如果是从暂停中恢复，更新状态
        if (this.paused && forumFavorFragment != null && forumFavorFragment.isResumed()) {
            this.paused = false;
            forumFavorFragment.updateStatus();
            forumFavorFragment.updateFavorForum();
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        this.paused = true;
    }
}